#include <stdint.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>
#include <stdio.h>

size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;
    //解析为json格式的字符串
    cJSON *root = cJSON_Parse(result);
    //做非空判断
    if(root!=NULL){
        cJSON *successRoot = cJSON_GetObjectItemCaseSensitive(root,"success");
        cJSON *codeRoot = cJSON_GetObjectItemCaseSensitive(root,"code");
        cJSON *messageRoot = cJSON_GetObjectItemCaseSensitive(root,"message");   
        
        printf("success=%s ,code=%d \n",successRoot->valuestring,codeRoot->valueint);

    }
    return realsize;
}

int main(int argc, char const *argv[])
{
    //初始化
    CURL *curl=  curl_easy_init();
    if (curl)
    {
        
        //设置请求地址
        curl_easy_setopt(curl,CURLOPT_URL,"http://47.109.65.208/config.json");
        //设置回调函数
        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writeCallback);
        //发送请求
        CURLcode code = curl_easy_perform(curl);
         //判断是否发送成功
        if (code!=CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(code));
        }
        //
        curl_easy_cleanup(curl);

    }
    




    return 0;
}
